package com.shaco.heroproject.volley;

/**
 * Created by dllo on 15/11/10.
 */

import android.graphics.Bitmap;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.shaco.heroproject.application.BaseApplication;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by dllo on 15/10/21.
 */
public class VolleySingleton {
    private static VolleySingleton instance;

    private RequestQueue queue;
    private ImageLoader imageLoader;

    private VolleySingleton() {
        queue = getQueue();
        imageLoader = new ImageLoader(queue,new DoubleCache());
    }

    private RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseApplication.getContext());
        }
        return queue;
    }

    public static VolleySingleton getVolleyIntance() {
        if (instance == null) {
            synchronized (VolleySingleton.class) {
                if (instance == null) {
                    instance = new VolleySingleton();
                }
            }
        }
        return instance;
    }

    private static final String TAG = "VolleySingLeton";
    public <T> void addRequest(Request<T> request) {
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void addRequest(Request<T> request, Objects tag) {
        request.setTag(tag);
        queue.add(request);
    }

    public void addRequest(String url,Response.Listener<String> complete,Response.ErrorListener error){
        StringRequest request = new StringRequest(url,complete,error){
            @Override
             public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        addRequest(request);
    }

    public void getImage(String url,Response.Listener<Bitmap> response,Response.ErrorListener error){
        ImageRequest request = new ImageRequest(url, response, 800, 500, Bitmap.Config.ARGB_8888,error) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

        };
        addRequest(request);
    }

    public void removeRequset(Object tag) {
        queue.cancelAll(tag);
    }
    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}